﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XBoshi.SecurityCommon;
using System.Xml.Serialization;
using System.IO;

namespace LicenseManager
{
    public partial class Form1 : Form
    {
        private LicenseClass lc=new LicenseClass();  
        
        
        public Form1()
        {
            InitializeComponent();
             lc.uid = Guid.NewGuid().ToString();
             lc.usingtimes = 10000;
             lc.validDate = DateTime.Now.AddYears(1);
             lc.isTempLincese = true;
             licenseClassBindingSource.Add(lc);
             lc.machinCode = MachineNumber.GetMachineNumber(Application.ExecutablePath);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
    
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            int keyType= ((int)lc.machinCode.Substring(0, 1).ToCharArray().FirstOrDefault())%3  ;
            String LicenseInfo = lc.GetXmlSerializer();

            //序列话数据
            byte[] keys = new MD5Class().GetKeys(keyType);  
            String firstDecrytonResult = MD5Class.MyDESCrypto(LicenseInfo, keys, null );
            String MachiCode = MachineNumber.GetMachineCodeByNum(lc.machinCode);
            keys = Encoding.ASCII.GetBytes(MachiCode);   
            String SecondDecrytonResult = MD5Class.MyDESCrypto(firstDecrytonResult, keys, null);
            this.textBox1.Text = SecondDecrytonResult;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            saveFileDialog1.FileName = lc.uid +".lic";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) {

              Stream sw=saveFileDialog1.OpenFile();
              byte[] b = Encoding.ASCII.GetBytes(textBox1.Text);
              sw.Write(b,0,b.Length);
              sw.Close();   
            }   

        }
    }
}
